﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace QL_NOI_TRU
{
    public partial class cbbMaNhanVien : DevExpress.XtraEditors.XtraForm
    {
        public cbbMaNhanVien()
        {
            InitializeComponent();
        }

        private void cbbMaNhanVien_Load(object sender, EventArgs e)
        {
            var kn = new Ketnoi(frmDangnhap.stringConn);
            gridControl1.DataSource = kn.ExcuteQuery("select MA_NV as 'Mã nhân viên',KHOA.TEN_KHOA as 'Tên khoa',MAPH as 'Mã phòng', STTCA as 'STT ca',NGAY as 'Ngày' from TRUC,KHOA where KHOA.MA_KHOA = TRUC.MA_KHOA");
            // do du lieu vao cbbMaNV
            var a = "select MA_NV,HOTEN from NHAN_VIEN";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbMaNV.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_NV"], dr["HOTEN"]));
            }
            dr.Close();

        }

        private void cbbMaNV_SelectedIndexChanged(object sender, EventArgs e)
        {
            var c = cbbMaNV.Text.Substring(0, 7).Trim();
            var a = "select NHAN_VIEN.MA_KHOA ,KHOA.TEN_KHOA from NHAN_VIEN, KHOA where NHAN_VIEN.MA_KHOA = KHOA.MA_KHOA and NHAN_VIEN.MA_NV = '"+c+"'";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbKhoa.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_KHOA"], dr["TEN_KHOA"]));
            }
            dr.Close();
        }

        private void cbbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            var c = cbbKhoa.Text.Substring(0, 2);
            var a = "select * from PHONG_TRUC where PHONG_TRUC.MA_KHOA = '"+c+"'";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbPhongTruc.Properties.Items.Add(String.Format("{0} | {1} | {2}", dr["MAPH"], dr["MA_KHOA"],dr["TEN_PHONG"]));
            }
            dr.Close();
        }

        private void cbbPhongTruc_SelectedIndexChanged(object sender, EventArgs e)
        {
            var a = "select * from CA";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbCa.Properties.Items.Add(String.Format("{0} | {1} | {2}", dr["STTCA"], dr["GIOBD"], dr["GIOKT"]));
            }
            dr.Close();
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            string strr = "EXEC Insert_LichTruc @MA_NV = '" + cbbMaNV.Text.Substring(0, 10).Trim() + "',@STTCA = '" + cbbCa.Text.ToString().Trim() + "',@NGAY = '" + cbbNgay.Text.ToString().Trim() + "',@MAPH = '" + cbbPhongTruc.Text.Substring(0, 4).Trim() + "',@MA_KHOA = '" + cbbKhoa.Text.Substring(0, 2).Trim() + "'";
            var kn = new Ketnoi(frmDangnhap.stringConn);
            kn.ExcuteQuery(strr);
            btRefresh_Click(null, null);
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            cbbKhoa.SelectedIndex = -1;
            cbbCa.SelectedIndex = -1;
            cbbMaNV.SelectedIndex = -1;
            cbbNgay.Text = "";
            cbbPhongTruc.SelectedIndex = -1;
            cbbMaNhanVien_Load(null, null);
        }
    }
}